/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.d.lib.common.widget;

/**
 * 动画工具类.
 */
public class AnimatorValueUtils {

  /**
   * 获取动画值.
   *
   * @param fraction 完成度
   * @param values   变化值
   * @return 动画值
   */
  public static float getAnimatedValue(float fraction, float... values) {
    if (values == null || values.length == 0) {
      return 0;
    }
    if (values.length == 1) {
      return values[0] * fraction;
    } else {
      if (fraction == 1) {
        return values[values.length - 1];
      }
      float oneFraction = 1f / (values.length - 1);
      float offFraction = 0;
      for (int i = 0; i < values.length - 1; i++) {
        if (offFraction + oneFraction >= fraction) {
          return values[i] + (fraction - offFraction) * (values.length - 1) * (values[i + 1] - values[i]);
        }
        offFraction += oneFraction;
      }
    }
    return 0;
  }

}
